package models;

import javax.persistence.*;
import java.util.*;
import play.db.jpa.Model;

/**
 * This class to represent a DB table "Notification" which has attributes sender
 * and receiver and the body of the notification itself (message) and path:
 * which directs to the page related to this notification and entityId in case
 * we needed to keep track of any IDs for each notification
 *  
 * @author Omnia
 * 
 */
@Entity
public class Notification extends Model {
	public long senderId;
	public long receiverId;
	public String message;
	public String path;
	public int type;
	public boolean seen;
	public Date notificationDate = new Date();

	public Notification(long senderId, long receiverId, long entityId,
			int type) {
		
		this.senderId = senderId;
		this.receiverId = receiverId;
		this.type = type;
		if (this.senderId == 0) {
			sytemNotification(entityId);
		} else 
		{
			RegisteredUser sender = RegisteredUser.findById(senderId);
			if (this.type >= 1 && this.type <= 10){
				this.tutorialRedirect(sender, entityId);
			}	
			else if(this.type >= 11 && this.type <= 15) {
				this.reviewRedirect(sender, entityId);
			}
			else if(this.type >= 16 && this.type <=19) {
				this.communityListRedierect(sender, entityId);
			}
			else if(this.type >= 20 && this.type <= 24) {
				this.sytemNotification(entityId);
			}
			else if (this.type == 25) {
				this.notifyHouseKeeper();
			}
			else if (this.type >= 26 && this.type <=29) {
				this.exerciseRedierect(sender, entityId);
			}
			else if(this.type >=30 && this.type <= 34) {
				this.profileRedirect(sender);
			}
		}
	}

	/**
	 * for the notifications where type >= 1 && type <= 10
	 * @param sender
	 * @param entityId
	 * @author Omnia
	 */
	private void tutorialRedirect(RegisteredUser sender, long entityId) {
		Tutorial tutorial = Tutorial.findById(entityId);
		this.path = "/Tutorials/tutorialPage?tutId=" + entityId;
		String title = tutorial.getRawTitle();
		switch (type) {
		case 1: // like tutorial >Omnia DONE!
			this.message = sender.name + " has liked " + title; break;
		case 2: // commented on Tutorial >Rana DONE!
			this.message = sender.name + " has commented on " + title; break;
		case 3: // make contributor > ebaa DONE!
			this.message = sender.name + " made you a contributor to "
					+ tutorial.Title; break;
		case 4: // rate tutorial > Rana Zakaria DONE!
			this.message = sender.name + " rated " + title; break;
		case 5: // follow tutorial > Marwa DONE!
			this.message = sender.name + " has followed " + title; break;
		case 6: // accept req. to edit > Marwa DONE!
			this.message = sender.name
					+ " has accepted your request to edit "
					+ title; break;
		case 7: // accept req. to contribute > Omnia DONE!
			this.message = sender.name
					+ " has accepted your request to contribute "
					+ title; break;
		case 8: // created Tutorial for review > Ghada
			this.message = sender.name + " has created a new tutorial "; break;
		case 9: // to be used later if needed
		case 10: // to be used later if needed

		}
	}

	/**
	 * for the notifications where type >= 11 && type <= 15
	 * @param sender
	 * @param entityId
	 * @author Omnia
	 */
	private void reviewRedirect(RegisteredUser sender, long entityId) {
		Review review = Review.findById(entityId);
		Tutorial revTutorial = Tutorial.findById(review.tutId);
		this.path = "/Reviews/viewReview?tutId="+entityId; 
		
		switch (type) {
		case 11: // send Review >Mahmoud DONE!
			this.message = sender.name + " has sent you a review on "
					+ revTutorial.getRawTitle();
			break;
		case 12: // creator/reviewer reply to review >Dina DONE!
			this.message = sender.name
					+ " has replied on the review on "
					+ revTutorial.getRawTitle();
			break;
		case 13: // creator/reviewer accepted review >Dina DONE!
			this.message = sender.name + " has accepted the review on "
					+ revTutorial.getRawTitle();
			break;
		case 14: // creator/reviewer rejected review >Dina DONE!
			this.message = sender.name + " has rejected the review on "
					+ revTutorial.getRawTitle();
			break;
		case 15: // to be used later if needed
		}
	}
	/**
	 * for notifications where type >= 16 && type <= 19
	 * @param sender
	 * @param entityId
	 * @author Omnia
	 */
	private void communityListRedierect(RegisteredUser sender, long entityId) {
		CommunityList commList = CommunityList.findById(entityId);
		this.path = "/CommunityLists/commListContent?Open=" + entityId;
		switch (type) { // assign commList Owner >Ebaa DONE

			case 16: this.message = sender.name
						+ " have assigned you to be the owner of "
						+ commList.name; break;
			case 17: // to be used later if needed
			case 18: // to be used later if needed
			case 19: // to be used later if needed
		}
	}

	/**
	 * for notifications where type <= 20 && <=24
	 * @param entityId
	 * @author Omnia
	 */
	private void sytemNotification(long entityId) {
		switch (type) { 
		case 20: // system assign owner > Ebaa DONE
			this.path = "/CommunityLists/commListContent?Open=" + entityId;
			CommunityList commList = CommunityList.findById(entityId);
			this.message = "You have been assigned to be the creator of "
					+ commList.name; break;
					
		case 21: // to be used later if needed
		case 22: // to be used later if needed
		case 23: // to be used later if needed
		case 24: // to be used later if needed

		}
	}

	/**
	 * for houseKeeper Notification 
	 * type == 25
	 * @author Omnia
	 */
	private void notifyHouseKeeper() {
		 // assign houseKeeper >Yasmine.gamal DONE!
		this.message = "You have been assigned as house keeper";
		this.path = "/Application/updates"; // yeroo7 ll home		
	}
	/**
	 * for notifications where type >= 26 and <=29
	 * @param sender
	 * @param entityId
	 * @author Omnia
	 */
	private void exerciseRedierect(RegisteredUser sender, long entityId) {
		Exercise exercise = Exercise.findById(entityId);
		this.path = "/ReviewRequests/myCreatedExercises";
		switch (type) { // assign commList Owner >Ebaa DONE

		case 26:
			this.message = sender.name + " has accepted review to "
					+ exercise.getExerciseName(); break;
		case 27:
			this.message = sender.name + " has rejected review to "
					+ exercise.getExerciseName(); break;
		case 28: // to be used later if needed
		case 29: // to be used later if needed
		}
	}
	/**
	 * for notification where type >= 30 and <= 34
	 * @param sender
	 * @author Omnia
	 */

	private void profileRedirect(RegisteredUser sender) {
		this.path = "/Application/othersProfile?viewOthers="+ sender.id;
		switch (type) { // assign commList Owner >Ebaa DONE

		case 30:
			this.message = sender.name + " has followed you";
			break;
		case 31: // to be used later if needed
		case 32: // to be used later if needed
		case 33: // to be used later if needed
		case 34: // to be used later if needed
	}
		
	}

	/**
	 * this is to get the message that should appear to the user
	 * 
	 * @return String message
	 * @author Omnia
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * this method returns the path where the user should be redirected to
	 * @return String path
	 * @author Omnia
	 */
	public String getPath() {
		return path;
	}

	/**
	 * this method to return the value of the seen 
	 * @return seen
	 * @author Omnia
	 */
	public boolean isSeen() {
		return seen;
	}

	/**
	 * this is to set seen with true
	 * @author Omnia
	 */
	public void setSeen() {
		seen = true;
	}


}
